package com.zte.cl.utils;

import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

@SuppressWarnings("deprecation")
public class HttpUtil {
	
	private static final int CONNECTION_TIMEOUT = 200 * 1000; // 连接200秒超时
	private static final int SO_TIMEOUT = 400 * 1000; // 读取数据400秒超时
	
	/**发送post请求，传递list参数
	 * 
	 * @param url 请求的url
	 * @param params 请求参数
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendPost(String url, List<BasicNameValuePair> params, boolean ifProxy) throws Exception {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		
		
		HttpPost request = new HttpPost(url);
		
		HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
		
		request.setEntity(entity);
		HttpResponse response = client.execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**发送post请求，传递json参数
	 * 
	 * @param url 请求的url
	 * @param json 请求参数
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendPost(String url, JSONObject json, boolean ifProxy) throws Exception {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		
		HttpPost request = new HttpPost(url);
		
		String jsonStr = "";
		if (json != null) {
			jsonStr = json.toString();
		}
		
		ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
		StringEntity entity = new StringEntity(jsonStr, contentType);
		
		request.setEntity(entity);
		HttpResponse response = client.execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**发送post请求，传递String参数
	 * 
	 * @param url 请求的url
	 * @param paramStr 请求参数
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendPost(String url, String paramStr, boolean ifProxy) throws Exception {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		
		HttpPost request = new HttpPost(url);
		
		ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
		StringEntity entity = new StringEntity(paramStr, contentType);
		
		request.setEntity(entity);
		
		HttpResponse response = client.execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**发送post请求，传递String参数
	 * 
	 * @param url 请求的url
	 * @param paramStr 请求参数
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendPost(String url, String paramStr, boolean ifProxy, boolean keepSession, HttpSession session) throws Exception {
		HttpClient client = null;
		if (keepSession) {
			Object httpClient = session.getAttribute("httpClient");
			if (httpClient != null) {
				client = (HttpClient) httpClient;
			} else {
				client = new DefaultHttpClient();
			}
		} else {
			client = new DefaultHttpClient();
		}
		
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		
		HttpPost request = new HttpPost(url);
		
		ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
		StringEntity entity = new StringEntity(paramStr, contentType);
		
		request.setEntity(entity);
		
		HttpResponse response = client.execute(request);
		
		if (keepSession) {
			session.setAttribute("httpClient", client);
		}
		
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**发送get请求
	 * 
	 * @param url 请求的url
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendGet(String url, boolean ifProxy) throws Exception {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		
		HttpGet request = new HttpGet(url);
		
		HttpResponse response = client.execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**发送DELETE请求，传递String参数
	 * 
	 * @param url 请求的url
	 * @param paramStr 请求参数
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendDelete(String url, String paramStr, boolean ifProxy) throws Exception {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		HttpDelete request = new HttpDelete(url);
//		ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
//		StringEntity entity = new StringEntity(paramStr, contentType);
		
		HttpResponse response = client.execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**
	 * 发送put请求, 传递json参数
	 * @param url 请求的url
	 * @param json 请求参数
	 * @param ifProxy 是否设置代理
	 * @return
	 * @throws Exception
	 */
	public static String sendPut(String url, JSONObject json, boolean ifProxy) throws Exception {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 连接10秒超时
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); // 读取数据20秒超时
		
		if (ifProxy) {
			//设置代理
			setProxy(client);
		}
		
		HttpPut request = new HttpPut(url);
		
		String jsonStr = "";
		if (json != null) {
			jsonStr = json.toString();
		}
		
		ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
		StringEntity entity = new StringEntity(jsonStr, contentType);
		
		request.setEntity(entity);
		HttpResponse response = client.execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		if (responseEntity != null) {
			return EntityUtils.toString(responseEntity);
		}
		
		return "";
	}
	
	/**
	 * 设置代理
	 */
	private static void setProxy(HttpClient client) {
		//设置代理
		String proxyHost = ConstantUtil.PROXY_HOST;
		int proxyPort = ConstantUtil.PROXY_PORT;
		HttpHost proxy = new HttpHost(proxyHost, proxyPort);
		client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
	}
	
	/**
	 * 设置代理
	 */
	@SuppressWarnings("unused")
	private static void setProxy(HttpRequestBase requestBase) {
		//设置代理
		String proxyHost = ConstantUtil.PROXY_HOST;
		int proxyPort = ConstantUtil.PROXY_PORT;
		HttpHost proxy = new HttpHost(proxyHost, proxyPort);
		requestBase.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
	}
}
